package com.mobimagic.lockscreen.view;

import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.TextView;

import com.mobimagic.lockscreen.AppEnv;

public class SlideTextView extends TextView implements Observer{
    private static final boolean DEBUG = AppEnv.bAppdebug;
    private static final String TAG = "SlideTextView";

    private static final int LIGHT_COLOR = Color.WHITE;
    private static final int TEXT_COLOR = 0x4Dffffff;
    private static final int ANIMATION_INTERVAL = 50;
    private static final int ANIMATION_SECTION = 20;

    private LinearGradient mLinearGradient;
    private Matrix mGradientMatrix;
    private Paint mPaint;
    private int mViewWidth = 0;
    private int mTranslate = 0;
    private int mLightColor = LIGHT_COLOR;
    private int mTextColor = TEXT_COLOR;

    public SlideTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    private void setShader() {
        mLinearGradient = new LinearGradient(-mViewWidth, 0, 0, 0, new int[] { mTextColor, mLightColor, mTextColor },
                new float[] { 0, 0.5f, 1f }, Shader.TileMode.CLAMP);
        if (mPaint == null) {
            mPaint = getPaint();
        }
        if (mGradientMatrix == null) {
            mGradientMatrix = new Matrix();
        }
        mPaint.setShader(mLinearGradient);
    }

    public void setTextColor(int color) {
        if (color != mTextColor) {
            this.mTextColor = color;
            setShader();
        }
    }

    public void setLightColor(int color) {
        if (color != mLightColor) {
            this.mLightColor = color;
            setShader();
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mViewWidth == 0) {
            mViewWidth = getMeasuredWidth();
            if (mViewWidth > 0) {
                setShader();
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mGradientMatrix != null) {
            mTranslate += mViewWidth / ANIMATION_SECTION;
            if (mTranslate >= 2 * mViewWidth) {
                mTranslate = 0;
            }
            mGradientMatrix.setTranslate(mTranslate, 0);
            mLinearGradient.setLocalMatrix(mGradientMatrix);
            postInvalidateDelayed(ANIMATION_INTERVAL);
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        if (observable != null && observable instanceof MobileChargeColorHelper && data != null
                && data instanceof ChargeColor) {
            ChargeColor chargeColor = (ChargeColor) data;
            setLightColor(chargeColor.getSlideViewCurrentColor());
        }
    }

}
